<?php
namespace app\admin\controller;
use framework\ext\Check;

class UserController extends \app\base\controller\AdminController{

	//用户列表
	public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.fid',0,intval);
            if($fid == 1){
                $this->jsonMsg('ID是“1”的系统管理员禁止删除!');
            } 
            $condition['id'] = $fid;
            $result =  model('admin')->_delete($condition);
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败');
            } 
        }else{
            $page = form('get.page',0,intval);
            $this->rel = model('admin')->pager($page,20)->_select();
            $this->pager = getPage(model('admin')->pager);
    		$this->display();
        }
	}

    //添加或编辑用户
    public function edit(){
        if(!$this->isPost()){
            $this->fid = form('get.fid',0,intval);
            $condition['id'] = $this->fid;
            $this->info = model('admin')->_find($condition);
            $this->website = model('website')->_select();
            $this->auth = model('auth')->_select();
            $this->display();
        }else{
            $fid = form('post.fid',0,intval);
            $username = form('post.username');
            $password = form('post.password');
            $chackpassword = trim($_POST['chackpassword']);
            $data['website_id'] = form('post.website_id',0,intval);
            $data['groupid'] = form('post.groupid',0,intval);
            if($fid){
                $msg = Check::rule(array(Check::must($username),'请输入用户名'));
                if($password){
                    $msg = Check::rule(
                        array(Check::must($chackpassword), '请输入确认密码'),
                        array(check::same($password,$chackpassword),'两次密码输入不相同'));
                    if(true !== $msg){
                        $this->jsonMsg($msg);
                    }
                    $data['password'] = md5($password);
                }
                $data['username'] = $username;
                $condition['id']= $fid;
                $rel = model('admin')->_update($data,$condition);
            }else{
                $msg = Check::rule(
                    array(Check::must($username),'请输入用户名'),
                    array(Check::must($password),'请输入密码'),
                    array(Check::must($chackpassword),'请输入密码'),
                    array(check::same($password,$chackpassword),'两次密码输入不相同'));
                if(true !== $msg){
                    $this->jsonMsg($msg);
                }
                $info = model('admin')->_find(array('username' => $username));
                if($info){
                    $this->jsonMsg('重复的管理员名称');
                }
                $data['username'] = $username;
                $data['password'] = md5($password);
                $data['login_time'] = time();
                $rel = model('admin')->_update($data);
            }
            if($rel){
                $this->jsonMsg('提交成功',1,url('user/index'));
            }else{
                $this->jsonMsg('提交失败,请返回重试');
            }
        }
    }

    //更改密码
    public function password(){
        if($this->isPost()){
            $password = form('post.password');
            $newpassword = form('post.newpassword');
            $chackpassword = form('post.chackpassword');
            $msg = Check::rule(
                array(Check::must($password), '请输入现在的密码') ,
                array(Check::must($newpassword), '请输入新密码'),
                array(Check::must($chackpassword), '请输入确认密码'),
                array(check::same($newpassword,$chackpassword),'两次新密码输入不相同'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $condition['id'] = $_SESSION['admin_userInfo']['id'];
            $rel = model('admin')->_find($condition,'password');
            $password = md5($password);
            if(!empty($rel) && $rel['password'] == $password){
                $data['password'] = md5($newpassword);
                model('admin')->_update($data,$condition);
                $this->jsonMsg('密码修改成功',1,url('default/welcome'));
            }else{
                $this->jsonMsg('密码修改失败');
            }
        }else{
            $this->login_info = $_SESSION[$this->appID . '_userInfo'];
            $this->display();
        }   
    }
}